package com.yz.lzq.liwushuo.ui.modules.home.nomal;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.config.CircleTransform;
import com.yz.lzq.liwushuo.jsonbean.home.boutique.HomeBoutiqueRecycleBean;
import com.yz.lzq.liwushuo.ui.modules.comminterface.OnMyItemClickListener;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/12/13.
 */

public class HomeNomalFragmentRecycleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private List<HomeBoutiqueRecycleBean.DataBean.ItemsBean> mList = null;
    private OnMyItemClickListener listener = null;
    private LayoutInflater inflater = null;
    private Context context = null;
    public static final int EMPTY_VIEW = 0;
    private static final int IMG_VIEW = 1;
    public static final int NOMAL_VIEW = 2;
    private AnimationDrawable drawable;

    public HomeNomalFragmentRecycleViewAdapter(Context context, List<HomeBoutiqueRecycleBean.DataBean.ItemsBean> mList, OnMyItemClickListener listener) {
        this.mList = mList;
        this.context = context;
        this.listener = listener;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getItemViewType(int position) {
        if (mList.size() <= 0) {
            return EMPTY_VIEW;
        } else if (null == mList.get(position).getAuthor() && null == mList.get(position).getColumn()) {
            return IMG_VIEW;
        } else {
            return NOMAL_VIEW;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case EMPTY_VIEW:
                view = inflater.inflate(R.layout.empty_view_recyclerview, null);
                return new EmptyViewHolder(view);
            case IMG_VIEW:
                view = inflater.inflate(R.layout.item_recyclerview_img, null);
                view.setOnClickListener(this);
                return new ImageViewHolder(view);
            case NOMAL_VIEW:
                view = inflater.inflate(R.layout.item_homejingpin_nomal_recycle, null);
                view.setOnClickListener(this);
                return new MyViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            if (null != drawable) {
                drawable.stop();
            }
            HomeBoutiqueRecycleBean.DataBean.ItemsBean itemsBean = mList.get(position);
            if (itemsBean.getAuthor() != null) {

                //加在圆形图像
                Glide.with(context).load(itemsBean.getAuthor().getAvatar_url())
                        .transform(new CircleTransform(context)).into(((MyViewHolder) holder).imageViewNomalItemHead);
                //加载用户名
                ((MyViewHolder) holder).textViewNomalItemName.setText(itemsBean.getAuthor().getNickname());
                //加载用户的描述
                ((MyViewHolder) holder).textViewNomalItemHobby.setText(itemsBean.getAuthor().getIntroduction());
            }
            //加载大型图片
            Glide.with(context).load(itemsBean.getCover_image_url()).into(((MyViewHolder) holder).imageViewNomalItemShow);
            //加载加粗标题
            if (null != itemsBean.getTitle()) {
                ((MyViewHolder) holder).textViewNomalItemTittle.setText(itemsBean.getTitle());
            }
            //加载描述的内容
            ((MyViewHolder) holder).textViewNomalItemContent.setText(itemsBean.getIntroduction());
            //加载商店
            if (null != itemsBean.getColumn()) {
                ((MyViewHolder) holder).textViewNomalItemStore.setText(itemsBean.getColumn().getTitle());
            } else {
                ((MyViewHolder) holder).textViewNomalItemLanmu.setVisibility(View.GONE);
            }
            if (null != (itemsBean.getLikes_count() + "")) {
                //加载喜欢量
                ((MyViewHolder) holder).textViewNomalItemNumber.setText(itemsBean.getLikes_count() + "");

            }


        } else if (holder instanceof EmptyViewHolder) {
            ((EmptyViewHolder) holder).imageViewEmptyView.setImageResource(R.drawable.empty_view_anim);
            drawable.start();

        } else if (holder instanceof ImageViewHolder) {
            Glide.with(context).load(mList.get(position).getCover_image_url()).into(((ImageViewHolder) holder).imageViewRecyclerViewItem);
        }
        //将数据保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public void onClick(View view) {
        if (null != listener) {
            //注意这里使用getTag方法获取数据  onItemClick(v,(DataModel)v.getTag());
            listener.clickMyItem(view, (int) view.getTag());
        }
    }


    static class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.imageView_nomal_item_head)
        ImageView imageViewNomalItemHead;
        @Bind(R.id.textView_nomal_item_name)
        TextView textViewNomalItemName;
        @Bind(R.id.textView_nomal_item_hobby)
        TextView textViewNomalItemHobby;
        @Bind(R.id.imageView_nomal_item_show)
        ImageView imageViewNomalItemShow;
        @Bind(R.id.textView_nomal_item_tittle)
        TextView textViewNomalItemTittle;
        @Bind(R.id.textView_nomal_item_content)
        TextView textViewNomalItemContent;
        @Bind(R.id.textView_nomal_item_store)
        TextView textViewNomalItemStore;
        @Bind(R.id.textView_nomal_item_number)
        TextView textViewNomalItemNumber;
        @Bind(R.id.textView_nomal_item_lanmu)
        TextView textViewNomalItemLanmu;

        MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    /**
     * 局不刷新RecyclerView
     *
     * @param _list
     * @param isClear
     */
    public void reloadRecyclerView(List<HomeBoutiqueRecycleBean.DataBean.ItemsBean> _list, boolean isClear) {
        if (isClear) {
            mList.clear();
        }
        mList.addAll(_list);
        notifyDataSetChanged();
    }

    static class EmptyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.imageView_emptyView)
        ImageView imageViewEmptyView;

        EmptyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.imageView_recyclerView_item)
        ImageView imageViewRecyclerViewItem;

        ImageViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
